package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xqyy.eval.model.TTestPaper;
import org.xqyy.eval.model.TestPaper;
import org.xqyy.eval.service.ITestPaperService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by luoxiaoming on 16-12-25.
 */
@Controller
@RequestMapping("/testPaper")
public class TestPaperController {

    @Autowired
    private ITestPaperService testPaperService;


    @RequestMapping("/selectAllPaper")
    @ResponseBody
    public  List<TTestPaper> selectAllPaper(){
        return  testPaperService.selectAllPaper();
    }

    @RequestMapping("/addPaper")
    @ResponseBody
    public  boolean addPaper(@RequestBody TTestPaper tTestPaper){
        try {
            testPaperService.addTestPaper(tTestPaper,tTestPaper.getQuestions());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/delPaper")
    @ResponseBody
    public  boolean delPaper(TTestPaper tTestPaper){
        try {
            testPaperService.delTestPaper(tTestPaper);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @RequestMapping("/findTestPaperById")
    @ResponseBody
    public  TTestPaper findTestPaperById(TTestPaper tTestPaper){
        return testPaperService.findTestPaperById(tTestPaper);
    }


    @RequestMapping("/updatePaper")
    @ResponseBody
    public  boolean updatePaper(@RequestBody TTestPaper tTestPaper){
        try {
            testPaperService.updateTestPaper(tTestPaper);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/addEvalDeptPaper")
    @ResponseBody
    public  boolean addEvalDeptPaper(String deptcode,String evalDeptCodeString,Integer papaerId ){
        try {
            String[]  evalDeptCodeArray =  evalDeptCodeString.split(",");
            List<String> evalDeptCodeList = new ArrayList<>(Arrays.asList(evalDeptCodeArray));
            return testPaperService.addEvalDeptPaper(deptcode,evalDeptCodeList,papaerId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/copyEvalDeptPaper")
    @ResponseBody
    public  boolean copyEvalDeptPaper(String deptid,String referdeptid){
        try {
            String[]  deptCodeArray =  deptid.split(",");
            List<String> deptCodeList  = new ArrayList<>(Arrays.asList(deptCodeArray));
            return testPaperService.copyEvalDeptPaper(referdeptid,deptCodeList);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/loadPapers")
    @ResponseBody
    public Map<String, Object> loadPapers(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        String type = request.getParameter("type");
        if (StringUtils.hasLength(type)) {
            res.put("type", type);
        }
        String paperId = request.getParameter("paperId");
        if (StringUtils.hasLength(paperId)) {
            res.put("paperId", paperId);
        }
        List<Map<String, Object>> data = testPaperService.load(res);
        res.put("root", data);
        return res;
    }







    @RequestMapping("/deletePaper")
    @ResponseBody
    public Map<String, Object> deletePaper(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String paperId = request.getParameter("paperId");
            res.put("paperId", paperId);
            testPaperService.delete(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/addDetail")
    @ResponseBody
    public Map<String, Object> addDetail(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String paperId = request.getParameter("paperId");
            String[] ids = request.getParameterValues("questionId");
            res.put("paperId", paperId);
            res.put("ids",ids);
            testPaperService.addDetail(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/deleteDetail")
    @ResponseBody
    public Map<String, Object> deleteDetail(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String paperId = request.getParameter("paperId");
            String[] ids = request.getParameterValues("questionId");
            res.put("paperId", paperId);
            res.put("ids",ids);
            testPaperService.deleteDetail(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }
}
